/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package PersistanceLayer;

import BusinessLayer.Ecue;
import BusinessLayer.Note;

import PersistanceLayer.DAO;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import java.util.ArrayList;

import jdbc.ConnectionToOracle;

/**
 *
 * @author Julien
 */
class NoteDAO extends DAO<Note>{

     private static final NoteDAO instance = new NoteDAO();

     /* Methode permettant de recuperer l'objet unique de type UEDAO
     */
    public final static NoteDAO getInstance() {
        return instance;
    }

    @Override
    public boolean create(Note obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean update(Note obj) {
        return true;
    }
    
    /**
     * 
     */
    public boolean update(String id, String codeMatiere, String noteSession1, String noteSession2) {
    	
    	try {
    		
    		java.sql.Statement state =this.connect.createStatement();
    		
			ResultSet res = this.connect.createStatement()
			.executeQuery("update note set notesession1='"+noteSession1+"' where id='"+id+"' and codematiere='"+ codeMatiere +"'" );
			
			
			res = this.connect.createStatement()
			.executeQuery("update note set notesession2='"+noteSession2+"' where id='"+id+"' and codematiere='"+ codeMatiere +"'" );

			state.close();
			res.close();
			return true;
    	}
    	catch(SQLException e){
    		return false;
    	}
    }
    
    @Override
    public boolean update(Note ancien, Note nouveau) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Note find(Note obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean delete(Note obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

   /**
    * 
    */
    public ArrayList<Note> getListe() {

            	String noteSession1;
            	String noteSession2;            	
            	    	
            	ArrayList<Note> ensNote = new ArrayList<Note>();

            	
            	try {
            		
            		java.sql.Statement state =this.connect.createStatement();
            		
        			ResultSet res = this.connect.createStatement()
        			.executeQuery("SELECT * FROM NOTE");
        		
        			
        			while(res.next()){	        				
        				
        				
        				if(res.getObject(3) != null)
        					noteSession1 = res.getObject(3).toString();
        				else
        					noteSession1 = "";
        				
        				if(res.getObject(4) != null)
        					noteSession2 = res.getObject(5).toString();
        				else
        					noteSession2 = "";
        				
        				
        				
        			ensNote.add(new Note(noteSession1, noteSession2));	
        				
        				
        			}
        			
        			state.close();
        			res.close();
        			
        			return ensNote;
            	} catch (SQLException e) {
        			// 
            		
        			e.printStackTrace();
        			return null;
        		}
            	
            }
    
    public ArrayList<Note> getListe(String codeMatiere) {

    	String noteSession1;
    	String noteSession2;
    	String ID;
    	    	
    	ArrayList<Note> ensNote = new ArrayList<Note>();

    	
    	try {
    		
    		java.sql.Statement state =this.connect.createStatement();
    		
    		ResultSet res = this.connect.createStatement()
			.executeQuery("SELECT * from note " +
					"where codematiere = '"+ codeMatiere +"'");
		
			ResultSetMetaData resultMeta = res.getMetaData();
			
			while(res.next()){	        				
				
				if(res.getObject(1) != null)
					ID = res.getObject(1).toString();
				else
					ID = "";
				if(res.getObject(2) != null)
					codeMatiere = res.getObject(3).toString();
				else
					codeMatiere = "";
				if(res.getObject(3) != null)
					noteSession1 = res.getObject(3).toString();
				else
					noteSession1 = "";
				
				if(res.getObject(4) != null)
					noteSession2 = res.getObject(4).toString();
				else
					noteSession2 = "";	
				
				
				ensNote.add(new Note(ID, codeMatiere,noteSession1, noteSession2));	
				
			}
			
			
			state.close();
			res.close();
			
			return ensNote;
    	} catch (SQLException e) {
			// 
    		
			e.printStackTrace();
			return null;
		}
    	
    	
    	
    }
    
  

	@Override
	public String getNom(String nomMatiere) {

		    	String nom = null;    	  	

		    	try {
		    		
		    		java.sql.Statement state =this.connect.createStatement();
		    		
		    		ResultSet res = this.connect.createStatement()
					.executeQuery("SELECT codematiere from ecue " +
							"where libelleecue = '"+ nomMatiere +"'");
				
					
					while(res.next()){	        				
						
						
						if(res.getObject(1) != null)
							nom = res.getObject(1).toString();
						else
							nom = "";   				
									
									
					}
					
					
					state.close();
					res.close();
					return nom;	
					
		    	} catch (SQLException e) {
					// 
		    		
					e.printStackTrace();
					return null;
				}
		    	
		    
	}
    


  /* public static void main(String[] args){
	
	Connection connexion;
	NoteDAO note = new NoteDAO();
	//ArrayList<Note> noteList = new ArrayList<Note>();
	//String nom;
	
	connexion = ConnectionToOracle.getInstance();
	//noteList=note.getListe("J2");
	//nom=note.getNom("java");
	//System.out.println(noteList.toString());
	System.out.println(update("3","A1","10","12"));
	}*/
    


}
